<?php

namespace App\Http\Controllers\Shop;

use App\Http\Controllers\ShopController;
use App\Models\Shop\GoodsClassify;
use App\Models\Shop\Recommend;
use App\Models\Shop\Slide;
use App\Models\Shop\Store;
use Illuminate\Http\Request;

use Illuminate\Support\Facades\DB;

class IndexController extends ShopController
{

    public function getIndex(Request $request)
    {
        $this->active = 'index';
        $storeID = \Illuminate\Support\Facades\Request::cookie('storeID') ? \Illuminate\Support\Facades\Request::cookie('storeID') : 1;
        $typeID = 0;
        $page = ($request->has('page') ? $request->input('page') : 1) - 1;
        $sort = $request->input('sort') ? $request->input('sort') : '';

        $goods = DB::table('shop_store_goods')
            ->join('shop_goods', 'shop_store_goods.goodsID', '=', 'shop_goods.id')
            ->where('shop_store_goods.storeID', $storeID)
            ->where('shop_store_goods.display', 1);
        if($request->has('typeID'))
        {
            $typeID = $request->input('typeID');
            $goods = $goods->where('shop_goods.classifyID',$typeID);
        }
        $goods = $goods->select('shop_store_goods.id', 'shop_store_goods.hit', 'shop_store_goods.storeID', 'shop_store_goods.amount', 'shop_store_goods.realAmount', 'shop_store_goods.number', 'shop_store_goods.display',
            'shop_goods.classifyID', 'shop_goods.title', 'shop_goods.thumb', 'shop_goods.abstract');
        if ($sort)
        {
            switch ($sort)
            {
                case 'lp':
                    //价格最低
                    $goods = $goods->orderBy('shop_store_goods.realAmount', 'asc');
                    break;
                case 'new':
                    //最新
                    $goods = $goods->orderBy('shop_store_goods.id', 'desc');
                    break;
                case 'hot':
                    $goods = $goods->orderBy('shop_store_goods.hit', 'desc');
                    break;
                default:
                    $goods = $goods->orderBy('shop_store_goods.id', 'desc')->orderByRaw('ec_shop_store_goods.hit desc');
            }
        }else{
            $goods = $goods->orderBy('shop_store_goods.id', 'desc')->orderByRaw('ec_shop_store_goods.hit desc');
        }
        $goods = $goods->skip($page*10)
            ->take(10)
            ->get();

        //获取分类
        $classify = GoodsClassify::all()->keyBy('id');
        //获取大学
        $store = Store::orderBy('id', 'desc')->get()->keyBy('id');
        //获取幻灯
        $slide = Slide::where('storeID', $storeID)->where('display', 1)->orderBy('id', 'desc')->get();
        //获取推荐位
        $recommend = Recommend::where('storeID', $storeID)->where('display', 1)->get();

        return response()->view('shopWx.index', ['recommend' => $recommend, 'active' => $this->active, 'goods' => $goods, 'classify' => $classify, 'store' => $store, 'storeID' => $storeID, 'typeID' => $typeID, 'slide' => $slide])->withCookie(cookie()->forever('storeID', $storeID));
    }

    public function getIndexAjax(Request $request)
    {
        $storeID = \Illuminate\Support\Facades\Request::cookie('storeID') ? \Illuminate\Support\Facades\Request::cookie('storeID') : 1;
        $typeID = 0;
        $page = ($request->has('page') ? $request->input('page') : 1) - 1;
        $sort = $request->input('sort') ? $request->input('sort') : '';

        $goods = DB::table('shop_store_goods')
            ->join('shop_goods', 'shop_store_goods.goodsID', '=', 'shop_goods.id')
            ->where('shop_store_goods.storeID', $storeID)
            ->where('shop_store_goods.display', 1);
        if($request->has('typeID'))
        {
            $typeID = $request->input('typeID');
            $goods = $goods->where('shop_goods.classifyID',$typeID);
        }
        $goods = $goods->select('shop_store_goods.id', 'shop_store_goods.hit', 'shop_store_goods.storeID', 'shop_store_goods.amount', 'shop_store_goods.realAmount', 'shop_store_goods.number', 'shop_store_goods.display',
            'shop_goods.classifyID', 'shop_goods.title', 'shop_goods.thumb', 'shop_goods.abstract');
        if ($sort)
        {
            switch ($sort)
            {
                case 'lp':
                    //价格最低
                    $goods = $goods->orderBy('shop_store_goods.realAmount', 'asc');
                    break;
                case 'new':
                    //最新
                    $goods = $goods->orderBy('shop_store_goods.id', 'desc');
                    break;
                case 'hot':
                    $goods = $goods->orderBy('shop_store_goods.hit', 'desc');
                    break;
                default:
                    $goods = $goods->orderBy('shop_store_goods.id', 'desc')->orderByRaw('ec_shop_store_goods.hit desc');
            }
        }else{
            $goods = $goods->orderBy('shop_store_goods.id', 'desc')->orderByRaw('ec_shop_store_goods.hit desc');
        }
        $goods = $goods->skip($page*10)
            ->take(10)
            ->get();

        //获取分类
        $classify = GoodsClassify::all()->keyBy('id');
        //获取大学
        $store = Store::orderBy('id', 'desc')->get()->keyBy('id');

        $ajaxCode = $goods ? 1 : 0;
        $ajaxMsg = $goods ? 'success' : 'error';
        $lazyImg = asset('style/shopWx/img/lazy.png');
        foreach ($goods as $k=>$v)
        {
            $goods[$k]->thumb = action('ImgController@getPicture', ['sourceName' => $v->thumb, 'size' => '110x90']);
            $goods[$k]->lazyImg = $lazyImg;
        }
        return json_encode([
            'code'      =>  $ajaxCode,
            'msg'       =>  $ajaxMsg,
            'data'      =>  [
                'goods'     =>  $goods
            ]
        ]);
    }

    public function postIndex(Request $request)
    {
        if ($request->has('page'))
        {
            $page = $request->input('page');
            $storeID = \Illuminate\Support\Facades\Request::cookie('storeID') ? \Illuminate\Support\Facades\Request::cookie('storeID') : 1;
            $typeID = $request->input('typeID');
            //获取cookie购物车
            $cartOld = [];
            if(isset($_COOKIE['cart']))
            {
                $cartCookie = json_decode($_COOKIE['cart']);
                if(count($cartCookie) > 0)
                {
                    foreach($cartCookie as $c)
                    {
                        $cartOld[$c[0]] = $c;
                    }
                }
            }

            //获取物品
            $goods = DB::table('shop_store_goods')
                ->join('shop_goods', 'shop_store_goods.goodsID', '=', 'shop_goods.id')
                ->where('shop_store_goods.storeID', $storeID)
                ->where('shop_store_goods.display', 1);
            if($typeID)
            {
                $goods = $goods->where('shop_goods.classifyID', $typeID);
            }
            $goods = $goods->select('shop_store_goods.id', 'shop_store_goods.storeID', 'shop_store_goods.amount', 'shop_store_goods.realAmount', 'shop_store_goods.number', 'shop_store_goods.display',
                'shop_goods.classifyID', 'shop_goods.title', 'shop_goods.thumb', 'shop_goods.abstract')
                ->skip($page*10)
                ->take(10)
                ->orderBy('id', 'desc')
                ->get();
            if (count($goods) > 0)
            {
                $html = '';

                foreach ($goods as $v)
                {
                    $html .= '<li id="goods-'.$v->id.'">';
                    $html .= '<a href="#" class="item-link item-content">';
                    $html .= '<div class="item-media"><img data-src="'.action('ImgController@getPicture', ['sourceName' => $v->thumb, 'size' => '80x80']).'" width="80" class="lazy lazy-fadein"></div>';
                    $html .= '<div class="item-inner">';
                    $html .= '<div class="item-title-row closeLink">';
                    $html .= '<div class="item-title">'.$v->title.'</div>';
                    $html .= '<div class="item-after">'.$v->realAmount.' 円</div>';
                    $html .= '</div>';
                    $html .= '<div class="item-text">'.$v->abstract.'</div>';
                    $html .= '</div></a>';
                    $html .= '<div class="form-group shop-add">';
                    $html .= '<div class="col-xs-6"></div><div class="col-xs-6"><p class="buttons-row">';
                    if(count($cartOld) > 0 && isset($cartOld[$v->id]))
                    {
                        $html .= '<a href="#" class="button down"><i class="fa fa-cart-arrow-down fa-2x"></i></a>';
                        $html .= '<span class="button" data-id="'.$v->id.'" data-amount="'.$v->realAmount.'">'.$cartOld[$v->id][2].'</span>';
                    }else{
                        $html .= '<a href="#" class="button down hidden"><i class="fa fa-cart-arrow-down fa-2x"></i></a>';
                        $html .= '<span class="button" data-id="'.$v->id.'" data-amount="'.$v->realAmount.'"></span>';
                    }
                    $html .= '<a href="#" class="button plus"><i class="fa fa-cart-plus fa-2x"></i></a>';
                    $html .= '</p></div></div>';
                    $html .= '</li>';
                }

                $result = [
                    'code'      =>      0,
                    'date'      =>      $html
                ];
            }else{
                $result = [
                    'code'      =>      1,
                    'date'      =>      ''
                ];
            }
            return json_encode($result);
        }
    }
}
